var add,edit,setVal,reset;
require(['select2', 'date','layout','validator'], function() {
	var $formData = $('#formData');
	$formData.initForm();
	
	add = function(fn) {
		addOrUpdate("/equipmentInfo/addEquipmentInfo.htm",fn);
	};
	
	edit = function(fn) {
		addOrUpdate("/equipmentInfo/uptEquipmentInfo.htm",fn);
	};
	
	setVal = function (eqmtId){
		$.ajaxExt({
			url :"/equipmentInfo/getEquipmentInfoByEqmtId.htm",
			data : {'eqmtId' : eqmtId}
		}).done(function(data){
			$formData.setValues(data);
			if(data.licPltNo){
                $("#rltvCarId").append("<option value='"+data.rltvCarId+"'>"+data.licPltNo+"</option>").select2('val',data.rltvCarId);
            }
		});
	};
	
	reset = function (){
		$formData.reset($formData);
		// 初始化生厂厂家和设备类型
		//$("#produceFactyDlg").trigger('change');
	};
	
	//级联
	$("#produceFactyDlg").on("change", function(event,callback){
		var produceFacty = $(this).val()
		var eqmtTypes = window.parent.GLOBAL_PARAMS.DICT_CACHE[''+produceFacty+''];
		var content="";
		$(eqmtTypes).each(function(i,item) {
			content+='<option value="'+item.key+'">'+item.value+'</option>';
		});
		//if(content==""){
		//	$("#eqmtTypeDlg").html('<option value="">无</option>');
		//}else{
		//	$("#eqmtTypeDlg").html(content);
		//}
		if(typeof callback === 'function'){
			callback();
		}
	});
	
	function addOrUpdate(url,callBack){
		var result = $.validator($formData);
		$.ajaxExt({
			url: '/baseOrgan/getBaseOrganList.htm'
		}).done(function(data){
			$.each(data, function(i, res){
				if($("input[name='organId']").val() == res.organId){
					$("input[name='corpId']").val(res.corpId);
					if(result){
			    		$.ajaxExt({
			    			url : url,
			    			data : $formData.getValues()
			    		}).done(function(){
			    			callBack();
			    		});
			    	}
					return;
				}
			});
		});
	}
});